﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using qlHieuThuocDongY.BusinessLogic;
using qlHieuThuocDongY.BusinessObject;

namespace qlHieuThuocDongY
{
    public partial class frmBanThuoc : Form
    {

        private BanThuocBUS banthuocBUS = new BanThuocBUS();
        private NhaCungCapBUS nhacungcapBUS = new NhaCungCapBUS();
        private DanhMucThuocBUS dmThuocBUS = new DanhMucThuocBUS();
        private thongTinBenhNhanBUS bnBUS = new thongTinBenhNhanBUS();

        public frmBanThuoc()
        {
            InitializeComponent();
        }

        private void frmBanThuoc_Load(object sender, EventArgs e)
        {

            // Load combox mã thuốc
            cboMaThuoc.DataSource = dmThuocBUS.LayDanhMucThuoc();
            cboMaThuoc.DisplayMember = "TenThuoc";
            cboMaThuoc.ValueMember = "MaThuoc";
            //Load combox mã bệnh nhân
            cboMaBN.DataSource = bnBUS.LayDanhSachBenhNhan();
            cboMaBN.DisplayMember = "TenBN";
            cboMaBN.ValueMember = "MaBN";
            // Load combox cho  mã thuốc trong DataGridView
            colMaThuoc.DataSource = dmThuocBUS.LayDanhMucThuoc();
            colMaThuoc.DisplayMember = "TenThuoc";
            colMaThuoc.ValueMember = "MaThuoc";
            // Load combox mã bệnh nhân trong DataGridView
            colMaBN.DataSource = bnBUS.LayDanhSachBenhNhan();
            colMaBN.DisplayMember = "TenBN";
            colMaBN.ValueMember = "MaBN";
            // load DataGridView sau cùng
            dataGridViewBanThuoc.DataSource = banthuocBUS.LayDanhSachThuocBan();
            EnableEditing(false);
        }

        private void dataGridViewBanThuoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // dòng hiện tại
            int dong = e.RowIndex;
            // thực thi binding data lên control, khi click vào row dataGridView
            txtMaHDBan.Text = dataGridViewBanThuoc.Rows[dong].Cells["MaHDBan"].Value.ToString();
            cboMaThuoc.SelectedValue = dataGridViewBanThuoc.Rows[dong].Cells["colMaThuoc"].Value.ToString();
            txtSoLuong.Text = dataGridViewBanThuoc.Rows[dong].Cells["SoLuong"].Value.ToString();
            dtPickerNgayBan.Text = dataGridViewBanThuoc.Rows[dong].Cells["NgayBan"].Value.ToString();
            cboMaBN.SelectedValue = dataGridViewBanThuoc.Rows[dong].Cells["colMaBN"].Value.ToString();

        }

        private void EnableEditing(bool editing)
        {
            //button
            btnThem.Enabled = !editing;
            btnSua.Enabled = !editing;
            btnXoa.Enabled = !editing;
            btnLuu.Enabled = editing;
            btnKhongLuu.Enabled = editing;
            // texBox,Combobox
            txtMaHDBan.Enabled = editing;
            cboMaThuoc.Enabled = editing;
            txtSoLuong.Enabled = editing;
            dtPickerNgayBan.Enabled = editing;
            cboMaBN.Enabled = editing;
            // DataGridView
            dataGridViewBanThuoc.Enabled = !editing;
        }


        private void ResetTextValue()
        {
            txtSoLuong.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
            ResetTextValue();
            txtMaHDBan.Text = banthuocBUS.NextID();
        }

        private void btnKhongLuu_Click(object sender, EventArgs e)
        {
            EnableEditing(false);
        }


        private BanThuoc LayBanThuoc()
        {
            BanThuoc banthuoc = new BanThuoc();
            banthuoc.MaHDBan = txtMaHDBan.Text;
            banthuoc.MaThuoc = cboMaThuoc.SelectedValue.ToString();
            // Chuyển đồi kiểu dữ liệu số
            banthuoc.SoLuong = Convert.ToDouble(txtSoLuong.Text);
            // chuyển đổi sang kiểu dử liệu DateTime
            banthuoc.NgayBan = Convert.ToDateTime(dtPickerNgayBan.Value.ToShortDateString());
            banthuoc.MaBN = cboMaBN.SelectedValue.ToString();
            return banthuoc;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            BanThuoc banthuoc = LayBanThuoc();
            // Nếu tồn tại mã bệnh nhân rồi thì là sửa
            if (banthuocBUS.CheckExists(banthuoc.MaHDBan))
            {
                if (banthuocBUS.SuaThuocBan(banthuoc))
                    frmBanThuoc_Load(sender, e);

            }
            else // Thêm
            {
                if (banthuocBUS.ThemNhapThuoc(banthuoc))
                    frmBanThuoc_Load(sender, e);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Bạn Có Muốn Xóa Hóa Đơn: " + txtMaHDBan.Text + "Không?", "Hỏi",
               MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (banthuocBUS.XoaBanThuoc(txtMaHDBan.Text))
                    // load lại form
                    frmBanThuoc_Load(sender, e);
        }

    }
}
